//
//  HX_CycleImage.m
//  UI16-UICollectionViewCycleImage
//
//  Created by 韩旭 on 16/8/4.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HX_CycleImage.h"
#define kCollectionView @"collectionView"
#define COUNT 1000
#import "UIImageView+WebCache.h"
@interface HX_CycleImage ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIPageControl *pageControll;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger itemInteger;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) NSInteger contentSection;
@end
@implementation HX_CycleImage

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self =  [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setUp];
    }
    return self;
}
- (void)setUp {
    [self creatCollectionView];
    [self creatPageController];
    [self addTimer];
}

- (void)creatPageController {
    _pageControll = [[UIPageControl alloc] init];
    _pageControll.backgroundColor = [UIColor clearColor];
    _pageControll.pageIndicatorTintColor = [UIColor blackColor];
    _pageControll.numberOfPages = 5;
    NSLog(@"%ld", self.arrDataSource.count);
    _pageControll.currentPageIndicatorTintColor = [UIColor orangeColor];
    [self addSubview:_pageControll];
}
- (void)addTimer {
//    创建一个timer,并添加到loop中
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer2) userInfo:nil repeats:YES];
    //    timer仅仅是创建, 并没有加在loop中
   _timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timer2) userInfo:nil repeats:YES];
    
//    runloop 运行循环
//   NSRunLoopCommonMode 和 NSRunLoopCommonModes
//    后者代表默认状态, scheduledTImerWithTImeInterval 添加为默认状态
//    前者为多种状态, 包含 默认 及 滚动
//     [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}
- (void)removeTime {
    //    取消掉timer 把他置空 调用invalidate
    [_timer invalidate];
    //    _timer 置空
    _timer = nil;
}

- (void)timer2 {
    //    直接判断与0相等有何问题
    //    //    直接判断与0相等有何问题
    if (_itemInteger == _arrDataSource.count) {
        _itemInteger = 0;
    }
    if (_arrDataSource > 0) {
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_itemInteger inSection:_contentSection] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    }
    if (_collectionView.contentOffset.x == 0) {
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_arrDataSource.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    } else if (_collectionView.contentOffset.x == _collectionView.bounds.size.width * (_arrDataSource.count - 1)){
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:0 animated:NO];
    }
    if (_itemInteger == 6) {
        _pageControll.currentPage = 0;
    } else {
        _pageControll.currentPage = _itemInteger - 1;
    }
    _itemInteger++;
    
}
- (void)creatCollectionView {
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = 1;
    _layout.minimumInteritemSpacing = 0;
    _layout.minimumLineSpacing = 0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor greenColor];
    _collectionView.showsHorizontalScrollIndicator = YES;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[HX_CycleImageCell class] forCellWithReuseIdentifier:kCollectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return COUNT;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrDataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HX_CycleImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    [cell.imageViewCycle sd_setImageWithURL:_arrDataSource[indexPath.item] placeholderImage:[UIImage new]];
//    NSString *string = _arrDataSource[indexPath.item][@"image"];
//    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", string]];
//    cell.lable.text = _arrDataSource[indexPath.item][@"title"];
    _itemInteger = indexPath.item;
    _contentSection = indexPath.section;
    if (indexPath.section == COUNT - 1) {
        [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:COUNT] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    }
    
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didControllerViewCell:cell:)] ) {
        [self.delegate didControllerViewCell:_arrDataSource[indexPath.item] cell:cell];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate != nil) {
        [self.delegate didControllerView:indexPath.item];
       
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _collectionView.frame = self.bounds;
    _layout.itemSize = self.bounds.size;
    [_collectionView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
    _pageControll.frame = CGRectMake(0, 0, 200, 20);
    _pageControll.center = CGPointMake(kWidth / 2, self.bounds.size.height - 20);
}
- (void)setArrDataSource:(NSArray *)arrDataSource {
    _arrDataSource = arrDataSource;
    [_collectionView reloadData];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    直接判断与0相等有何问题
    if (scrollView.contentOffset.x == 0) {
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_arrDataSource.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    } else if (scrollView.contentOffset.x == _collectionView.bounds.size.width * (_arrDataSource.count - 1)){
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:0 animated:NO];
    }
}
@end

@implementation HX_CycleImageCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imageViewCycle = [[UIImageView alloc] initWithFrame:self.bounds];
        _lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width * 0.4, self.bounds.size.height * 0.15)];
        _lable.center = CGPointMake(self.bounds.size.width / 2, 40);
        _lable.textColor = [UIColor purpleColor];
        _lable.textAlignment = 1;
        _lable.font = [UIFont systemFontOfSize:20];
        _lable.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_imageViewCycle];
        [self.contentView addSubview:_lable];
    }
    return self;
}
@end